<template>
  <div class="document-favorites">
    <div class="favorites-header">
      <h1 class="favorites-title">我的收藏</h1>
      <div class="favorites-stats">
        <span class="stats-text">共 {{ favorites.length }} 个收藏</span>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <div class="loading-spinner">加载中...</div>
      <p>加载收藏列表中...</p>
    </div>

    <!-- 空状态 -->
    <div v-else-if="favorites.length === 0" class="empty-state">
      <div class="empty-icon">暂无</div>
      <h3>暂无收藏文档</h3>
      <p>收藏你喜欢的文档，方便日后快速查找</p>
      <router-link to="/doc" class="back-btn">
        返回文档列表
      </router-link>
    </div>

    <!-- 收藏列表 -->
    <div v-else class="favorites-list">
      <div 
        v-for="favorite in favorites" 
        :key="favorite.id" 
        class="favorite-card"
        @click="selectDocument(favorite)"
      >
        <div class="card-header">
          <h3 class="document-title">{{ favorite.name }}</h3>
          <div class="card-actions">
            <button 
              @click.stop="removeFavorite(favorite)" 
              class="remove-btn" 
              title="取消收藏"
              :disabled="removing === favorite.id"
            >
              {{ removing === favorite.id ? '删除中' : '删除' }}
            </button>
          </div>
        </div>
        <div class="card-meta">
          <span class="meta-item">
            <span class="meta-label">创建时间：</span>
            <span class="meta-value">{{ formatDate(favorite.createdTime) }}</span>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import type { Doc } from '@/modules/doc/types/doc.ts'
import { useDoc } from '@/modules/doc/composables'
import { toast } from '@/utils/message.ts'

const router = useRouter()

const { 
  documents: favorites, 
  loading, 
  fetchFavoriteDocuments,
  updateDocument
} = useDoc()

const removing = ref<string | null>(null)

const formatDate = (dateString: string | undefined): string => {
  if (!dateString) {
    return '未知'
  }
  
  // 直接返回原字符串，不进行任何解析
  return dateString
}

// 加载收藏列表
const loadFavorites = async () => {
  try {
    await fetchFavoriteDocuments()
  } catch (error: any) {
    console.error('加载收藏列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 取消收藏
const removeFavorite = async (favorite: Doc) => {
  const confirmed = await toast.confirm(
    `确定要取消收藏文档"${favorite.name}"吗？`,
    {
      title: '取消收藏',
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }
  )

  if (!confirmed) return

  removing.value = favorite.id
  try {
    // 使用 composable 的更新方法
    await updateDocument(favorite.id, {
      ...favorite,
      isFavorite: false
    })
    // 重新加载收藏列表
    await fetchFavoriteDocuments()
  } catch (error: any) {
    console.error('取消收藏失败:', error)
  } finally {
    removing.value = null
  }
}

// 选择文档
const selectDocument = (favorite: Doc) => {
  router.push(`/doc/${favorite.id}`)
}

// 组件挂载时加载收藏列表
onMounted(() => {
  // 设置页面标题
  window.document.title = '我的收藏 - 懒熊文档'
  
  loadFavorites()
})

// 暴露刷新方法给父组件
defineExpose({
  loadFavorites
})
</script>

<style scoped>
.document-favorites {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f8f9fa;
}

.favorites-header {
  padding: 2rem;
  background: white;
  border-bottom: 1px solid #e1e1e1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.favorites-title {
  margin: 0;
  color: #2c3e50;
  font-size: 1.8rem;
  font-weight: 600;
}

.favorites-stats {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.stats-text {
  color: #666;
  font-size: 0.9rem;
  background: #f8f9fa;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  border: 1px solid #e1e1e1;
}

.loading-state, .empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2rem;
}

.loading-spinner, .empty-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.empty-state h3 {
  margin: 0 0 0.5rem 0;
  color: #2c3e50;
  font-size: 1.5rem;
}

.empty-state p {
  margin: 0 0 2rem 0;
  color: #666;
  font-size: 1rem;
}

.back-btn {
  padding: 0.8rem 2rem;
  background: #42b883;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.2s;
  text-decoration: none;
  display: inline-block;
}

.back-btn:hover {
  background: #369870;
  color: white;
  text-decoration: none;
}

.favorites-list {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 1.5rem;
  align-content: start;
}

.favorite-card {
  background: white;
  border: 1px solid #e1e1e1;
  border-radius: 8px;
  padding: 1.5rem;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.favorite-card:hover {
  border-color: #42b883;
  box-shadow: 0 4px 12px rgba(66, 184, 131, 0.15);
  transform: translateY(-2px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
}

.document-title {
  margin: 0;
  color: #2c3e50;
  font-size: 1.2rem;
  font-weight: 600;
  flex: 1;
  word-break: break-word;
}

.card-actions {
  display: flex;
  gap: 0.5rem;
  opacity: 0;
  transition: opacity 0.2s;
}

.favorite-card:hover .card-actions {
  opacity: 1;
}

.remove-btn {
  padding: 0.4rem;
  background: #fd79a8;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s;
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.remove-btn:hover:not(:disabled) {
  background: #e84393;
}

.remove-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.card-meta {
  padding-top: 1rem;
  border-top: 1px solid #f0f0f0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.meta-label {
  color: #666;
}

.meta-value {
  color: #2c3e50;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .favorites-header {
    padding: 1rem;
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
  }

  .favorites-title {
    font-size: 1.4rem;
  }

  .favorites-list {
    padding: 1rem;
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .favorite-card {
    padding: 1rem;
  }

  .document-title {
    font-size: 1.1rem;
  }

  .card-actions {
    opacity: 1;
  }

  .card-header {
    flex-direction: column;
    gap: 0.5rem;
    align-items: flex-start;
  }
}

/* 深色主题支持 */
.dark .document-favorites {
  background: #1a1a1a;
}

.dark .favorites-header {
  background: #2d2d2d;
  border-bottom-color: #3d3d3d;
}

.dark .favorites-title {
  color: #e6e6e6;
}

.dark .stats-text {
  color: #b3b3b3;
  background: #3d3d3d;
  border-color: #4a4a4a;
}

.dark .favorite-card {
  background: #2d2d2d;
  border-color: #3d3d3d;
}

.dark .favorite-card:hover {
  border-color: #42b883;
  box-shadow: 0 4px 12px rgba(66, 184, 131, 0.2);
}

.dark .document-title {
  color: #e6e6e6;
}

.dark .card-meta {
  border-top-color: #3d3d3d;
}

.dark .meta-label {
  color: #b3b3b3;
}

.dark .meta-value {
  color: #e6e6e6;
}

.dark .empty-state h3 {
  color: #e6e6e6;
}

.dark .empty-state p {
  color: #b3b3b3;
}
</style>